package com.service.impl;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.CollectionUtils;

import com.common.Common;
import com.common.ReturnCode;
import com.dao.MenuDao;
import com.dao.SysOperateLogDao;
import com.model.ReponseDto;
import com.model.SysMenu;
import com.model.SysOperateLog;
import com.service.IMenuService;

@Service
public class MenuServiceImpl implements IMenuService {

    @Autowired
    private MenuDao menuDao;

    @Autowired
    private SysOperateLogDao sysOperateLogDao;

    @Override
    public ReponseDto<List<SysMenu>> list(Map<String, Object> parMap) {
        return new ReponseDto<>(menuDao.get(parMap));
    }

    @Override
    public ReponseDto<String> add(SysMenu sysMenu, String userId, String userName) {
        if (StringUtils.isEmpty(sysMenu.getName())) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        Map<String, Object> parMap = new HashMap<String, Object>();
        parMap.put("name", sysMenu.getName());
        List<SysMenu> slist = menuDao.get(parMap);
        if (!CollectionUtils.isEmpty(slist)) {
            return new ReponseDto<>(ReturnCode.MENU_NAME_REPEAT);
        }
        menuDao.insert(sysMenu);

        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("菜单管理-添加菜单");
        sysOperateLog.setDataCcope("菜单管理-添加菜单");
        sysOperateLogDao.insert(sysOperateLog);
        return new ReponseDto<>();
    }

    @Override
    public ReponseDto<String> update(SysMenu sysMenu, String userId, String userName) {
        if (null == sysMenu.getId() || StringUtils.isEmpty(sysMenu.getName())) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        Map<String, Object> parMap = new HashMap<String, Object>();
        parMap.put("id", sysMenu.getId());
        List<SysMenu> slist = menuDao.get(parMap);
        if (CollectionUtils.isEmpty(slist)) {
            return new ReponseDto<>(ReturnCode.DATA_NOT_EXISTS);
        }
        if (!StringUtils.equals(sysMenu.getName(), slist.get(0).getName())) {
            Map<String, Object> cmap = new HashMap<String, Object>();
            cmap.put("name", sysMenu.getName());
            List<SysMenu> clist = menuDao.get(cmap);
            if (!CollectionUtils.isEmpty(clist)) {
                return new ReponseDto<>(ReturnCode.MENU_NAME_REPEAT);
            }
        }
        menuDao.edit(sysMenu);
        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_ADD);
        sysOperateLog.setDescription("菜单管理-编辑菜单");
        sysOperateLog.setDataCcope("菜单管理-编辑菜单");
        sysOperateLogDao.insert(sysOperateLog);
        return new ReponseDto<>();
    }

    @Override
    public ReponseDto<String> delete(Long id, String userId, String userName) {
        if (null == id) {
            return new ReponseDto<>(ReturnCode.PARAM_ERROR);
        }
        
        menuDao.deleteRoleMenuByMenuId(id);
        
        menuDao.delete(id);
        
        SysOperateLog sysOperateLog = new SysOperateLog();
        sysOperateLog.setUserId(Long.valueOf(userId));
        sysOperateLog.setUserName(userName);
        sysOperateLog.setActiveType(Common.ACTIVE_TYPE_DELETE);
        sysOperateLog.setDescription("菜单管理-删除菜单");
        sysOperateLog.setDataCcope("菜单管理-删除菜单");
        sysOperateLogDao.insert(sysOperateLog);

        return new ReponseDto<>();
    }

}
